package net.jblog.struts.frontoffice.action;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jblog.business.ServiceLocatorReference;
import net.jblog.business.facade.PostFacade;
import net.jblog.data.model.Post;
import net.jblog.struts.backoffice.form.PostForm;
import net.jblog.struts.tools.BeanUtils;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class IndexAction extends GeneralAction {
	@SuppressWarnings("unused")
	private Logger log = Logger.getLogger(IndexAction.class.getName());
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PostFacade facade = ServiceLocatorReference.getServiceLocator().getPostFacade();
		List<Post> posts = facade.findLastPost(5);
		
		List<PostForm> postForms = new ArrayList<PostForm>();
		for (Post post : posts) {
			PostForm postForm = new PostForm();
			BeanUtils.copyPropertiesFromPostBeanToPostForm(post, postForm);
			postForms.add(postForm);
		}
		request.setAttribute("posts", postForms);
		

		// Gestion des modules
		manageModules(request);
		
		return mapping.findForward("success");
	}
	
	
}
